//
//  YCNetWorkTool.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/17.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit
import AFNetworking

enum YCNetworkError: Int {

    case emptyAccessToken = -1  // AccessToken没有值
    case emptyUid = -2  // Uid没有值
    
    var errorDescription: String {
        get {
            // 判断不同的类型返回不同的错误信息
            switch self {
            case YCNetworkError.emptyAccessToken:
                return "access_token没有值"
            case YCNetworkError.emptyUid:
                return "uid没有值"
            }
        }
    }
    
     func error()->NSError {
    
        return NSError(domain: "error.networking", code: self.rawValue, userInfo: ["errorDescription":self.errorDescription])
    
    }
    
    

}



class YCNetWorkTool: AFHTTPSessionManager {
    
    //MARK: - 单例对象
    static let shardNetWorkTool: YCNetWorkTool = {
        
        let url = NSURL(string: "https://api.weibo.com/")
        let shard = YCNetWorkTool(baseURL: url)
    shard.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return shard
    
    }()

    
    //MARK: - 加载用户数据
    func loadUserData(finish: (result: [String:AnyObject]?,error: NSError?) ->Void){
    
        
        let urlStr = "2/statuses/home_timeline.json"
        
       guard let access_token = YCUserAccount.loadUserAccount()?.access_token else
       {
            let error = YCNetworkError.emptyAccessToken.error()
            finish(result: nil, error: error)
        
            return
        
        }
        
        let parameters = [
        
            "access_token":access_token
        ]
        
        
        GET(urlStr, parameters: parameters, success: { (_, result) -> Void in
            
            let dic = result as! [String:AnyObject]
            
       
            finish(result: dic, error: nil)
            
            }) { (_, error) -> Void in
                
                finish(result: nil, error: error)
        }
        
        
    
    }
    
    
    let client_id = "1955383904"
    let redirect_uri = "http://www.baidu.com/"
    let client_secret = "c0cdf5f01fb97a0ecaf3a68500aaa656"
    //MARK: - 返回授权地址
    func backAuthorizeURL() ->NSURL {
        
            let urlStr = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        
        return NSURL(string: urlStr)!
    }
 
    //MARK: - POST方式发送Code,得到access_token数据
    func loadAccessToken(code: String,finish:(result:[String:AnyObject]?,error:NSError?) ->Void ){
    
        let urlStr = "oauth2/access_token"
        
        //请求的参数，看API文档
        let parameters = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
        
        POST(urlStr, parameters: parameters, success: { (_, result) -> Void in
            
            
            
            let dict = result as! [String:AnyObject]
            //闭包完成回调
            finish(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                
            finish(result: nil, error: error)
                print("error: \(error)")
        }
        
    }
    //MARK: - 加载用户信息
    func loadUserInfo(finish: (result: [String:AnyObject]?,error: NSError?) ->()){
    
        //守卫,没有值就执行else里面的
        guard let access_token = YCUserAccount.loadUserAccount()?.access_token else{
        
            
            let error = YCNetworkError.emptyAccessToken.error()
            
            
            
            return
        }
        guard let uid = YCUserAccount.loadUserAccount()?.uid else{
        
            return
        }
        
        let url = "2/users/show.json"
        
        let parameters = [
        "access_token":access_token,
            "uid":uid
        ]
        
        
        GET(url, parameters: parameters, success: { (_, result) -> Void in
            
            let dic = result as? [String:AnyObject]
           finish(result: dic, error: nil)
            
            }) { (_, error) -> Void in
                finish(result: nil, error: error)
        }
        
    }
    
}
